package com.example.calculator

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.Spinner
import android.widget.TextView
import android.widget.AdapterView


import android.widget.AdapterView.OnItemSelectedListener
import java.math.BigDecimal


class UnitConverterActivity : AppCompatActivity(), View.OnClickListener,
    AdapterView.OnItemClickListener {
var spinner1:String = ""
var spinner2:String = ""
    var spinner3:String = ""
    var spinner4:String = ""
    private val inputv: Spinner by lazy { findViewById<Spinner>(R.id.inputv) }
    private val inputv0: TextView by lazy { findViewById<TextView>(R.id.inputv0) }
    private val outputv: Spinner by lazy { findViewById<Spinner>(R.id.outputv) }
    private val outputv0: TextView by lazy { findViewById<TextView>(R.id.outputv0) }
    private val input: Spinner by lazy { findViewById<Spinner>(R.id.input) }
    private val input0: TextView by lazy { findViewById<TextView>(R.id.input0) }
    private val output: Spinner by lazy { findViewById<Spinner>(R.id.output) }
    private val output0: TextView by lazy { findViewById<TextView>(R.id.output0) }
    private val num9: Button by lazy { findViewById<Button>(R.id.num9) }
    private val num8: Button by lazy { findViewById<Button>(R.id.num8) }
    private val num7: Button by lazy { findViewById<Button>(R.id.num7) }
    private val num6: Button by lazy { findViewById<Button>(R.id.num6) }
    private val num5: Button by lazy { findViewById<Button>(R.id.num5) }
    private val num4: Button by lazy { findViewById<Button>(R.id.num4) }
    private val num3: Button by lazy { findViewById<Button>(R.id.num3) }
    private val num2: Button by lazy { findViewById<Button>(R.id.num2) }
    private val num1: Button by lazy { findViewById<Button>(R.id.num1) }
    private val num0: Button by lazy { findViewById<Button>(R.id.num0) }
    private val dian: Button by lazy { findViewById<Button>(R.id.dian) }
    private val CE: Button by lazy { findViewById<Button>(R.id.CE) }

    override fun onCreate(savedInstanceState: Bundle?){
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_unit_converter)
        input0.setOnClickListener(this)
//        input.setOnItemClickListener(this)
//        output.setOnItemClickListener()
        output0.setOnClickListener(this)
        num0.setOnClickListener(this)
        num1.setOnClickListener(this)
        num2.setOnClickListener(this)
        num3.setOnClickListener(this)
        num4.setOnClickListener(this)
        num5.setOnClickListener(this)
        num6.setOnClickListener(this)
        num7.setOnClickListener(this)
        num8.setOnClickListener(this)
        num9.setOnClickListener(this)
        CE.setOnClickListener(this)
        dian.setOnClickListener(this)
        inputv0.setOnClickListener(this)
        outputv0.setOnClickListener(this)

        //获取array中定义的值
        var value1 = resources.getStringArray(R.array.unit1)
        var value2 = resources.getStringArray(R.array.unit3)
        //添加Spinner监听事件
        input.setOnItemSelectedListener(object : OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View,
                position: Int,
                id: Long
            ) {
                spinner1 = value1.get(position)
                Log.i("spinner1",spinner1)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {}
        })
        output.setOnItemSelectedListener(object : OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View,
                position: Int,
                id: Long
            ) {
                spinner2 = value1.get(position)
                Log.i("spinner2",spinner2)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {}
        })

        inputv.setOnItemSelectedListener(object : OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View,
                position: Int,
                id: Long
            ) {
                spinner3 = value2.get(position)
                Log.i("spinner3",spinner3)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {}
        })

        outputv.setOnItemSelectedListener(object : OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View,
                position: Int,
                id: Long
            ) {
                spinner4 = value2.get(position)
                Log.i("spinner4",spinner4)
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {}
        })

    }
    override fun onClick(v: View?) {
        when(v?.id){
            R.id.num0->{
                input0.setText(input0.text.toString()+"0")
                inputv0.setText(inputv0.text.toString()+"0")
                output0.setText("")
                calclutor(input0.text.toString())
            }
            R.id.num1->{
                input0.setText(input0.text.toString()+"1")
                inputv0.setText(inputv0.text.toString()+"1")
                output0.setText("")
                calclutor(input0.text.toString())
            }
            R.id.num2->{
                input0.setText(input0.text.toString()+"2")
                inputv0.setText(inputv0.text.toString()+"2")
                output0.setText("")
                calclutor(input0.text.toString())
            }
            R.id.num3->{
                input0.setText(input0.text.toString()+"3")
                inputv0.setText(inputv0.text.toString()+"3")
                output0.setText("")
                calclutor(input0.text.toString())
            }
            R.id.num4->{
                input0.setText(input0.text.toString()+"4")
                inputv0.setText(inputv0.text.toString()+"4")
                output0.setText("")
                calclutor(input0.text.toString())
            }
            R.id.num5->{
                input0.setText(input0.text.toString()+"5")
                inputv0.setText(inputv0.text.toString()+"5")
                output0.setText("")
                calclutor(input0.text.toString())
            }
            R.id.num6->{
                input0.setText(input0.text.toString()+"6")
                inputv0.setText(inputv0.text.toString()+"6")
                output0.setText("")
                calclutor(input0.text.toString())
            }
            R.id.num7->{
                input0.setText(input0.text.toString()+"7")
                inputv0.setText(inputv0.text.toString()+"7")
                output0.setText("")
                calclutor(input0.text.toString())
            }
            R.id.num8->{
                output0.setText("")
                input0.setText(input0.text.toString()+"8")
                inputv0.setText(inputv0.text.toString()+"8")
                calclutor(input0.text.toString())            }
            R.id.num9->{
                output0.setText("")
                input0.setText(input0.text.toString()+"9")
                inputv0.setText(inputv0.text.toString()+"9")
                calclutor(input0.text.toString())
            }
            R.id.CE->{
                input0.setText("")
                output0.setText("")
                inputv0.setText("")
                outputv0.setText("")
            }
            R.id.dian->{
                input0.setText(input0.text.toString()+".")
                inputv0.setText(inputv0.text.toString()+".")
            }
        }
    }

    override fun onItemClick(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
        TODO("Not yet implemented")
    }

fun calclutor(str:String){
    var number1:BigDecimal=0.toBigDecimal()
    var number:BigDecimal=0.toBigDecimal()
    when(spinner1){
        "纳米/nm"->{
            when(spinner2){
                "纳米/nm"->number=str.toBigDecimal()
                "微米/um"->{
                    number=str.toBigDecimal()*0.001.toBigDecimal()
                }
                "毫米/mm"->{
                    number=str.toBigDecimal()*0.000001.toBigDecimal()
                }
                "厘米/cm"->{
                    number=str.toBigDecimal()*0.0000001.toBigDecimal()
                }
                "米/m"->{
                    number=str.toBigDecimal()*0.000000001.toBigDecimal()
                }
                "公里/km"->{
                    number=str.toBigDecimal()*0.000000000001.toBigDecimal()
                }

            }
        }
        "微米/um"->{
            when(spinner2){
                "纳米/nm"->number=str.toBigDecimal()*1000.toBigDecimal()
                "微米/um"->{
                    number=str.toBigDecimal()
                }
                "毫米/mm"->{
                    number=str.toBigDecimal()*0.001.toBigDecimal()
                }
                "厘米/cm"->{
                    number=str.toBigDecimal()*0.0001.toBigDecimal()
                }
                "米/m"->{
                    number=str.toBigDecimal()*0.000001.toBigDecimal()
                }
                "公里/km"->{
                    number=str.toBigDecimal()*0.000000001.toBigDecimal()
                }

            }
        }
        "毫米/mm"->{
            when(spinner2){
                "纳米/nm"->number=str.toBigDecimal()*1000000.toBigDecimal()
                "微米/um"->{
                    number=str.toBigDecimal()*1000.toBigDecimal()
                }
                "毫米/mm"->{
                    number=str.toBigDecimal()
                }
                "厘米/cm"->{
                    number=str.toBigDecimal()*0.1.toBigDecimal()
                }
                "米/m"->{
                    number=str.toBigDecimal()*0.001.toBigDecimal()
                }
                "公里/km"->{
                    number=str.toBigDecimal()*0.000001.toBigDecimal()
                }
            }
        }
        "厘米/cm"->{
            when(spinner2){
                "纳米/nm"->number=str.toBigDecimal()*10000000.toBigDecimal()
                "微米/um"->{
                    number=str.toBigDecimal()*10000.toBigDecimal()
                }
                "毫米/mm"->{
                    number=str.toBigDecimal()*10.toBigDecimal()
                }
                "厘米/cm"->{
                    number=str.toBigDecimal()
                }
                "米/m"->{
                    number=str.toBigDecimal()*0.01.toBigDecimal()
                }
                "公里/km"->{
                    number=str.toBigDecimal()*0.00001.toBigDecimal()
                }

            }
        }
        "米/m"->{
            when(spinner2){
                "纳米/nm"->number=str.toBigDecimal()*1000000000.toBigDecimal()
                "微米/um"->{
                    number=str.toBigDecimal()*1000000.toBigDecimal()
                }
                "毫米/mm"->{
                    number=str.toBigDecimal()*1000.toBigDecimal()
                }
                "厘米/cm"->{
                    number=str.toBigDecimal()*100.toBigDecimal()
                }
                "米/m"->{
                    number=str.toBigDecimal()
                }
                "公里/km"->{
                    number=str.toBigDecimal()*0.001.toBigDecimal()
                }
            }
        }
        "公里/km"->{
            when(spinner2){
                "纳米/nm"->number=str.toBigDecimal()*1000000000000.toBigDecimal()
                "微米/um"->{
                    number=str.toBigDecimal()*1000000000.toBigDecimal()
                }
                "毫米/mm"->{
                    number=str.toBigDecimal()*1000000.toBigDecimal()
                }
                "厘米/cm"->{
                    number=str.toBigDecimal()*100000.toBigDecimal()
                }
                "米/m"->{
                    number=str.toBigDecimal()*1000.toBigDecimal()
                }
                "公里/km"->{
                    number=str.toBigDecimal()
                }
            }
        }
    }

    when(spinner3){
        "立方厘米"->{
            when(spinner4){
                "立方厘米"-> number1=str.toBigDecimal()
                "立方米" -> number1=str.toBigDecimal()*0.000001.toBigDecimal()
                "立方千米"->number1=str.toBigDecimal()*0.000000000000001.toBigDecimal()
            }
        }
        "立方米"->{
            when(spinner4) {
                "立方厘米" -> number1 = str.toBigDecimal() * 1000000.0.toBigDecimal()
                "立方米" -> number1 = str.toBigDecimal()
                "立方千米" -> number1 = str.toBigDecimal() * 0.000000001.toBigDecimal()
            }
        }
        "立方千米"->{
            when(spinner4){
                "立方厘米" -> number1 = str.toBigDecimal() * 1000000000000000.0.toBigDecimal()
                "立方米" -> number1 = str.toBigDecimal()*1000000000.0.toBigDecimal()
                "立方千米" -> number1 = str.toBigDecimal() * 0.000000001.toBigDecimal()
            }
        }

    }

    output0.setText(number.toString())
    outputv0.setText(number1.toString())
}
    }